package com.headline.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.headline.pojo.Headline;
import com.headline.pojo.vo.PortalVo;
import com.headline.service.HeadlineService;
import com.headline.mapper.HeadlineMapper;
import com.headline.utils.JwtHelper;
import com.headline.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Aloha
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-10-05 14:48:53
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{

    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /*
    根据请求查询头条，自定义sql并且需要分页
    返回List<Map>，List存每条数据对应的Map，Map存查询到的每条数据的字段值
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {

        IPage<Map> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        headlineMapper.selectMyPage(page, portalVo);

        List<Map> list = page.getRecords();
        Map data = new HashMap();
        data.put("pageData", list);
        data.put("pageNum", page.getCurrent());
        data.put("pageSize", page.getSize());
        data.put("totalPage", page.getPages());
        data.put("totalSize", page.getTotal());

        Map pageInfo = new HashMap();
        pageInfo.put("pageInfo", data);
        return Result.ok(pageInfo);
    }

    /*
    根据id查询详情
    1、阅读量 + 1
    2、查询对应数据
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {
        Map data = headlineMapper.queryDetailMap(hid);

        Map headline = new HashMap();
        headline.put("headline", data);

        // 阅读量加一
        Headline headline1 = new Headline();
        headline1.setHid((Integer) data.get("hid"));
        /*
        setVersion不可省略，需要把version值传入，乐观锁才能发挥作用
        底层是生成一条新的sql语句
        UPDATE headline SET pageViews = #{pageViews}, version = version + 1
        WHERE hid = #{hid} AND version = #{version}
        需要传入更新前的version作为where对比条件，看与当前的version是否一致
         */
        headline1.setVersion((Integer) data.get("version"));
        headline1.setPageViews((Integer) data.get("pageViews") + 1);
        headlineMapper.updateById(headline1);

        return Result.ok(headline);
    }

    /*
    发布头条
     */
    @Override
    public Result publish(Headline headline, String token) {

        // 根据token查询用户id
        int userId = jwtHelper.getUserId(token).intValue();
        // 数据装配
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());

        // 数据插入
        headlineMapper.insert(headline);

        return Result.ok(null);
    }

    /*
    修改头条数据
    1、由于前端不会提供version，所以首先需要根据hid查询最新的version
    2、修改数据的修改时间为当前时间
     */
    @Override
    public Result updateHeadline(Headline headline) {

        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        headline.setVersion(version);
        headlineMapper.updateById(headline);

        return Result.ok(null);
    }
}




